<template>
  <div class="auther_table_wrap">
    <el-table
      :data="options"
      border
      style="width: 95.2%; margin: 0px auto"
      size="medium"
    >
      <el-table-column prop="id" label="id" width="100"> </el-table-column>
      <el-table-column prop="value" label="规格选项值"> </el-table-column>
      <el-table-column prop="spec" label="规格id"> </el-table-column>
      <el-table-column prop="spec_name" label="规格名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="fnPopShow(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="fnDelSpecs(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改规格" :visible.sync="pop_show" append-to-body>
      <el-form
        :model="optionsForm"
        status-icon
        :rules="rulesSpecsForm"
        ref="optionsForm"
        label-width="100px"
      >
        <el-form-item label="规格值：" prop="name">
          <el-input
            type="text"
            v-model="optionsForm.name"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="规格名称：" prop="spec_id">
          <el-select v-model="optionsForm.spec_id" size="small">
            <el-option
              v-for="item in specs_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm('optionsForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import cons from "@/components/constant";
let token = localStorage.token;
export default {
  name: "OptionsTable",
  props: ["options"],
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("规格名不能为空"));
      } else {
        callback();
      }
    };
    return {
      pop_show: false,
      edit_id: "",
      specs_list: [],
      optionsForm: {
        name: "",
        spec_id: "",
      },
      rulesSpecsForm: {
        name: [{ validator: validateName, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 点击编辑触发的方法
    fnPopShow(id) {
      this.pop_show = true;
      this.edit_id = id;
      this.axios
        .get(cons.apis + "/goods/specOptions/" + this.edit_id + "/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          console.log("点击编辑触发的方法", dat);
          this.optionsForm.name = dat.data.value;
          this.optionsForm.spec_id = dat.data.spec;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    // 提交触发的方法
    submitForm() {
      this.axios
        .put(
          cons.apis + "/goods/specOptions/" + this.edit_id + "/",
          {
            value: this.optionsForm.name,
            spec: this.optionsForm.spec_id,
          },
          {
            headers: {
              Authorization: "JWT " + token,
            },
            responseType: "json",
          }
        )
        .then((dat) => {
          console.log("点击提交触发的方法", dat);
          this.pop_show = false;
          this.$emit('fnResetTable');
          this.resetForm('optionsForm');
          if(dat.status == 201){
          this.$message({
            type: 'success',
            message: dat.data.msg
          });

          }else{
            this.$message({
            type: 'error',
            message: '修改失败!'
          });
          // this.pop_show = false;
          // this.$emit('fnResetTable');
          // this.resetForm('optionsForm');
          }
        })
        .catch((err) => {
            this.$message({
            type: "error",
            message: "修改失败",
          });
        });
    },
    fnDelSpecs(id) {
      this.edit_id = id;
      this.$confirm("此操作将删除该规格选项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete(cons.apis + "/goods/specOptions/" + this.edit_id + "/", {
              headers: {
                Authorization: "JWT " + token,
              },
              responseType: "json",
            })
            .then((dat) => {
              if (dat.status == 201) {
                this.$message({
                  type: "success",
                  // message: '删除成功!'
                  message: dat.data.msg,
                });
                this.$emit("fnResetTable");
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败!",
                });
                this.$emit("fnResetTable");
              }
            })
            .catch((err) => {
              if (err.response.status == 404) {
                this.$message({
                  type: "info",
                  message: "权限未找到！",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取所有的spu规格数据
    fnGetSpecs() {
      this.axios
        .get(cons.apis + "/goods/spuSpeclist/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          console.log("加载spu规格的响应", dat);
          this.specs_list = dat.data;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    this.fnGetSpecs();
  },
};
</script>

<style scope>
.el-table th,
.el-table td {
  text-align: center;
}
</style>
